//
//  CrazyChatMineTextCell.m
//  webSocket
//
//  Created by 马军凯 on 2019/2/15.
//  Copyright © 2019年 ZH. All rights reserved.
//

#import "CrazyChatMineTextCell.h"

@interface CrazyChatMineTextCell ()

@property (nonatomic, strong) UIImageView *mineHeadImageView; //头像
@property (nonatomic, strong) UIView *mineTextBackView; //背景
@property (nonatomic, strong) UIImageView *mineTextBackImageView; //背景图片
@property (nonatomic, strong) UILabel *mineTextLabel; //消息

@end

@implementation CrazyChatMineTextCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifier = CrazyChatMineTextIdentifier;
    
    //缓存中取
    CrazyChatMineTextCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    //创建
    if (!cell) {
        cell = [[CrazyChatMineTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        //头像
        self.mineHeadImageView = [[UIImageView alloc] init];
        self.mineHeadImageView.frame = CGRectMake(CRAZY_SCREEN_WIDTH - 55, 10, 40, 40);
        [self.contentView addSubview:self.mineHeadImageView];
        //背景
        self.mineTextBackView = [[UIView alloc] init];
        self.mineTextBackView.frame = CGRectMake(60, 10, CRAZY_SCREEN_WIDTH - 120, 40);
        [self.contentView addSubview:self.mineTextBackView];
        //背景图片
        self.mineTextBackImageView = [[UIImageView alloc] init];
        self.mineTextBackImageView.image = [[UIImage imageNamed:@"Crazy_chat_rightPic@2x.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(23, 15, 7, 15) resizingMode:UIImageResizingModeStretch];
        [self.mineTextBackView addSubview:self.mineTextBackImageView];
        //label
        self.mineTextLabel = [[UILabel alloc] init];
        self.mineTextLabel.textColor = CRAZY_RGBR(33);
        self.mineTextLabel.font = [UIFont systemFontOfSize:14];
        self.mineTextLabel.numberOfLines = 0;
        [self.mineTextBackView addSubview:self.mineTextLabel];
        //重发按钮
        self.mineTextRepeatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.mineTextRepeatBtn setImage:[UIImage imageNamed:@"Crazy_chat_repeat.png"] forState:UIControlStateNormal];
        self.mineTextRepeatBtn.size = CGSizeMake(30, 30);
        [self.contentView addSubview:self.mineTextRepeatBtn];
    }
    return self;
}

//赋值
- (void)setCellValueWithHeadURL:(NSString *)headURL content:(NSMutableAttributedString *)content repeatType:(NSString *)repeatType {
    //头像
    [self.mineHeadImageView sd_setImageWithURL:[NSURL URLWithString:headURL] placeholderImage:CRAZY_DEFAULT_HEADIMAGE];
    //聊天
    self.mineTextLabel.attributedText = content;
    CGSize maxSize = CGSizeMake(CRAZY_SCREEN_WIDTH - 120 - 20, 9999);
    CGSize size = [self.mineTextLabel sizeThatFits:maxSize];
    self.mineTextLabel.frame = CGRectMake(8, 10, size.width, size.height);
    //背景
    self.mineTextBackView.width = size.width + 20;
    self.mineTextBackView.height = (size.height + 20) < 40 ? 40 : (size.height + 20);
    self.mineTextBackView.x = 60 + CRAZY_SCREEN_WIDTH - 120 - self.mineTextBackView.width;
    //背景图片
    self.mineTextBackImageView.frame = self.mineTextBackView.bounds;
    //重发按钮
    self.mineTextRepeatBtn.y = self.mineTextBackView.height / 2 - 15 + 10;
    self.mineTextRepeatBtn.x = self.mineTextBackView.x - 30 - 10;
    //重发按钮
    if ([repeatType isEqualToString:@"1"]) {
        self.mineTextRepeatBtn.hidden = NO;
    } else {
        self.mineTextRepeatBtn.hidden = YES;
    }
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
